/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.common.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import ar.uba.fi.posgrado.academy.model.Course;
import ar.uba.fi.posgrado.academy.model.Coursing;

/**
 * Alumno
 * 
 * @author pablo.tortorella
 * @author lautaro.gutierrrez
 * 
 */

@Entity
@Table(name = "ALUMNO")
@SuppressWarnings("serial")
// @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Student extends AbstractDomainObject {

	@Column(name = "NOMBRE", length = 45)
	private String name;

	@Column(name = "APELLIDO", length = 45)
	private String surname;

	@Column(name = "DOCUMENTO", length = 25)
	private String personalId;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_TIPO_DOCUMENTO")
	private IdentificationType idType;

	@Column(name = "NACIONALIDAD", length = 25)
	private String nationality;

	@Column(name = "TELEFONO", length = 25)
	private String phone;

	@Column(name = "TELEFONO_CELULAR", length = 25)
	private String mobile;

	@Column(name = "TELEFONO_LABORAL", length = 25)
	private String workPhone;

	@Column(name = "EMAIL", length = 75)
	private String email;

	@Column(name = "EMAIL2", length = 75)
	private String email2;

	@Column(name = "FECHA_ALTA")
	private Date registerDate;

	@Embedded
	@AttributeOverrides( {
			@AttributeOverride(name = "street", column = @Column(name = "DIRECCION_CALLE")),
			@AttributeOverride(name = "number", column = @Column(name = "DIRECCION_NUMERO")),
			@AttributeOverride(name = "floor", column = @Column(name = "DIRECCION_PISO")),
			@AttributeOverride(name = "room", column = @Column(name = "DIRECCION_DEPARTAMENTO")),
			@AttributeOverride(name = "city", column = @Column(name = "DIRECCION_CIUDAD")),
			@AttributeOverride(name = "town", column = @Column(name = "DIRECCION_LOCALIDAD")),
			@AttributeOverride(name = "zipcode", column = @Column(name = "DIRECCION_CODIGO_POSTAL")) })
	private Address address;

	@Column(name = "EMPRESA", length = 50)
	private String company;

	@Column(name = "CARGO_EMPRESA", length = 50)
	private String position;

	@Embedded
	@AttributeOverrides( {
			@AttributeOverride(name = "degree", column = @Column(name = "TITULO", length = 100)),
			@AttributeOverride(name = "university", column = @Column(name = "UNIVERSIDAD_TITULO", length = 50)),
			@AttributeOverride(name = "year", column = @Column(name = "ANIO_TITULO")) })
	private Degree degree = new Degree();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
	private Set<Coursing> coursings = new HashSet<Coursing>();

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "students")
	private Set<Course> courses = new HashSet<Course>();

	@Column(name = "COMPLETO", nullable = false)
	private Boolean complete;

	public Boolean getComplete() {
		return complete;
	}

	public void setComplete(Boolean complete) {
		this.complete = complete;
	}

	/**
	 * @return the workPhone
	 */
	public String getWorkPhone() {
		return workPhone;
	}

	/**
	 * @param workPhone
	 *            the workPhone to set
	 */
	public void setWorkPhone(String workPhone) {
		this.workPhone = workPhone;
	}

	/**
	 * @return the email2
	 */
	public String getEmail2() {
		return email2;
	}

	/**
	 * @param email2
	 *            the email2 to set
	 */
	public void setEmail2(String email2) {
		this.email2 = email2;
	}

	/**
	 * @return the idType
	 */
	public IdentificationType getIdType() {
		return idType;
	}

	/**
	 * @param idType
	 *            the idType to set
	 */
	public void setIdType(IdentificationType idType) {
		this.idType = idType;
	}

	/**
	 * @return the degree
	 */
	public Degree getDegree() {
		return degree;
	}

	/**
	 * @param degree
	 *            the degree to set
	 */
	public void setDegree(Degree degree) {
		this.degree = degree;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @param mobile
	 *            the mobile to set
	 */
	public void setMobile(String mobileNumber) {
		this.mobile = mobileNumber;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phoneNumber) {
		this.phone = phoneNumber;
	}

	/**
	 * @return the nationality
	 */
	public String getNationality() {
		return nationality;
	}

	/**
	 * @param nationality
	 *            the nationality to set
	 */
	public void setNationality(String nacionality) {
		this.nationality = nacionality;
	}

	/**
	 * @return the registerDate
	 */
	public Date getRegisterDate() {
		return registerDate;
	}

	/**
	 * @param registerDate
	 *            the registerDate to set
	 */
	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}

	/**
	 * @return the address
	 */
	public Address getAddress() {
		return address;
	}

	public String getCompleteAddress() {
		StringBuffer response = new StringBuffer();
		if (address == null) {
			response.append("");
		} else if (address.getStreet() != null) {
			response.append(address.getStreet());
			if (address.getNumber() != null) {
				response.append(" ");
				response.append(address.getNumber());
			}
		}
		return response.toString();
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(Address address) {
		this.address = address;
	}

	public String getFullName() {
		return name + " " + surname;
	}

	public String getCorporateName() {
		return name + " " + surname + " (" + personalId + ")";
	}

	public String getSurnamePlusName() {
		return this.surname + ", " + this.name;
	}

	public String getSurnamePlusNameAndPersonalId() {
		return surname + ", " + name + " (" + personalId + ")";
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the surname
	 */
	public String getSurname() {
		return surname;
	}

	/**
	 * @param surname
	 *            the surname to set
	 */
	public void setSurname(String surname) {
		this.surname = surname;
	}

	/**
	 * @return the personalId
	 */
	public String getPersonalId() {
		return personalId;
	}

	/**
	 * @param personalId
	 *            the personalId to set
	 */
	public void setPersonalId(String personalId) {
		this.personalId = personalId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ar.uba.fi.posgrado.common.model.AbstractDomainObject#getExcludedFields()
	 */
	@Override
	public String[] getExcludedFields() {
		return new String[] { "phone", "mobile", "coursings" };
	}

	public Set<Coursing> getCoursings() {
		return coursings;
	}

	public void setCoursings(Set<Coursing> coursings) {
		this.coursings = coursings;
	}

	public String getStreet() {
		String response = "";
		if (this.getAddress() != null) {
			response = this.getAddress().getStreet();
		}
		return response;
	}

	public void setStreet(String street) {
		if (this.getAddress() != null) {
			this.address = new Address();
		}
		this.address.setStreet(street);
	}

	public String getNumber() {
		String response = "";
		if (this.getAddress() != null) {
			response = this.getAddress().getNumber();
		}
		return response;
	}

	public void setNumber(String number) {
		if (this.getAddress() != null) {
			this.address = new Address();
		}
		this.address.setNumber(number);
	}

	public Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}

	/**
	 * @return the position
	 */
	public String getPosition() {
		return position;
	}

	/**
	 * @param position
	 *            the position to set
	 */
	public void setPosition(String charge) {
		this.position = charge;
	}

	/**
	 * @return the company
	 */
	public String getCompany() {
		return company;
	}

	/**
	 * @param company
	 *            the company to set
	 */
	public void setCompany(String company) {
		this.company = company;
	}
}
